import util from '../../../utils/util'
var Crypto = require('../../../utils/cryptojs/cryptojs.js').Crypto;
const app = getApp();

Page({
  data: {
    options:[],
    logo:"../../image/bg.png",
    header:'',
    windowWidth: 0,
    windowHeight: 0,
    contentHeight: 0,
    thinkList: [],
    footer: '发送了一个口令红包',
    offset: 0,
    lineHeight: 30,
    content: '王小波的黄金时代有一片紫色的天空，天上飘着懒洋洋的云，他有好多奢望，想爱，想吃，想和陈清扬敦伟大的友谊。'
  },

  onLoad: function (options) {
    let that = this;
    console.log(options);
    that.setData({
      options:options,
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowWidth: res.windowWidth,
          windowHeight: res.windowHeight-60,
          offset: res.windowWidth / 2 - 80
        });
      }
    });
    that.setData({
      userInfo:app.globalData.userInfo,
    })
    wx.getStorage({
      key: 'token',
      success: function(res) {
        console.log("湖区")
        var token = res.data;
        var timestamp = app.timestamp();
       var  url = app.globalData.localApi + "/redpack/wxacode"
        wx.request({
          url: url,
          method: "POST",
          data: {
            token:token,
            timestamp:timestamp,
            sign: Crypto.MD5(app.sign(url, token, timestamp)),
            redpackId:that.data.options.orderno,
            url:app.urlStr(url),
          },
          success:function(res){
            console.log(res)
            if(res.data.code==201){
              app.login();
            }else if(res.data.success){

            }else{
              app.showInfo(res.data.msg)
            }
          }
        })
      },
    })

  },

  onShow: function () {
    this.getData()
  },

  getData: function () {
    let that = this;

    let i = 0;
    let lineNum = 1;
    let thinkStr = '';
    let thinkList = [];
    for (let item of that.data.content) {
      if (item === '\n') {
        thinkList.push(thinkStr);
        thinkList.push('a');
        i = 0;
        thinkStr = '';
        lineNum += 1;
      } else if (i === 19) {
        thinkList.push(thinkStr);
        i = 1;
        thinkStr = item;
        lineNum += 1;
      } else {
        thinkStr += item;
        i += 1;
      }
    }
    thinkList.push(thinkStr);
    that.setData({ thinkList: thinkList });
    that.createNewImg(10);
  },

  drawSquare: function (ctx) {
    ctx.rect(0, 0, this.data.windowWidth, this.data.windowHeight);
    ctx.setFillStyle("#ffffff");
    ctx.fill()
  },
  
  drawHeader:function(ctx,src,left,top,width,height){
      ctx.drawImage(src,left,top,width,height)  
  },

  drawFont: function (ctx, content, height) {
    ctx.setFontSize(18);
    ctx.setFillStyle("yellow");
    ctx.fillText(content, this.data.offset, height);
  },

  drawLine: function (ctx, height) {
    ctx.beginPath();
    ctx.moveTo(this.data.offset, height);
    ctx.lineTo(this.data.windowWidth - this.data.offset, height);
    ctx.stroke('#eee');
    ctx.closePath();
  },

  createNewImg: function (lineNum) {
    let that = this;
    let ctx = wx.createCanvasContext('myCanvas');
    console.log(lineNum)
    let contentHeight = lineNum * that.data.lineHeight + 180;
    that.drawSquare(ctx);
    that.setData({ contentHeight: contentHeight });
    // let height = 100;
    // for (let item of that.data.thinkList) {
    //   if (item !== 'a') {
    //     that.drawFont(ctx, item, height);
    //     height += that.data.lineHeight;
    //   }
    // }
    var Hleft = (that.data.windowWidth/2-40);
    var mleft = (that.data.windowWidth / 2-60);
    console.log("left:"+Hleft)
    that.drawHeader(ctx, that.data.logo, 0, -30, that.data.windowWidth, contentHeight);
    that.drawHeader(ctx, that.data.userInfo.avatarUrl, Hleft, 30, 80, 80);
    that.drawHeader(ctx, that.data.userInfo.avatarUrl, mleft, 260, 120, 120);
    // circleImg(ctx, that.data.userInfo.avatarUrl, 50,60,40);
    // that.drawHeader(ctx,that.data.userInfo.avatarUrl,80,60 ,40);
    // that.drawLine(ctx, lineNum * that.data.lineHeight + 120);
    that.drawFont(ctx, that.data.footer,150 );
    // ctx.drawImage('../../image/bg.png', that.data.windowWidth - that.data.offset - 50, lineNum * that.data.lineHeight + 125, 50, 50);
    ctx.draw();



    function circleImg(ctx, img, x, y, r) {
    console.log("执行circleImg")
    console.log(img)
      ctx.save();
      var d = 2 * r;
      var cx = x + r;
      var cy = y + r;
      ctx.arc(cx, cy, r, 0, 2 * Math.PI);
      ctx.clip();
      ctx.drawImage(img, x, y, d, d);
      ctx.restore();
    }
  },

  savePic: function () {
    let that = this;
    wx.canvasToTempFilePath({
      x: 0,
      y: 50,
      width: that.data.windowWidth,
      height: that.data.contentHeight,

      canvasId: 'myCanvas',
      success: function (res) {
        util.savePicToAlbum(res.tempFilePath)
      }
    })
  },


});